package com.guimin.demo.api.font;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.guimin.demo.constant.ServiceConstant;
import com.guimin.demo.entity.CommunityService;
import com.guimin.demo.entity.CommunityServiceMap;
import com.guimin.demo.service.CommunityServiceMapService;
import com.guimin.demo.service.CommunityServiceService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 桂民社区服务信息表(GuiminService)表控制层
 *
 * @author Administrator
 */
@RestController
@RequestMapping("/shequ/service")
public class CommunityServiceFontApi {

    /**
     * 服务对象
     */
    @Resource
    private CommunityServiceService serviceService;
    @Resource
    private CommunityServiceMapService communityServiceMapService;

    /**
     * 获取社区服务
     *
     * @param communityId
     * @return
     */
    @GetMapping("/get/list")
    public R<List<CommunityService>> getCommunityServiceList(@RequestParam Integer communityId) {
        List<CommunityService> serviceList = serviceService.list(new LambdaQueryWrapper<CommunityService>()
                .select(CommunityService::getId, CommunityService::getParentId, CommunityService::getType,
                        CommunityService::getName, CommunityService::getPrice)
                .ne(CommunityService::getState, ServiceConstant.State.DELETED.value)
        );
        List<CommunityServiceMap> mapList = communityServiceMapService.list(new LambdaQueryWrapper<CommunityServiceMap>()
                .eq(CommunityServiceMap::getCommunityId, communityId)
        );
        for (CommunityService service : serviceList) {
            if (service.getParentId() == null) {
                service.setParentId(0);
            }
            for (CommunityServiceMap one : mapList) {
                if (one.getServiceId().equals(service.getId())) {
                    service.setState(ServiceConstant.State.OPEN.value);
                    break;
                }
            }
        }
        return R.ok(serviceList);
    }
}
